package com.xdb.sqlparser.dialect.mysql.ast.statement;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.xdb.sqlparser.ast.SQLCommentHint;
import com.xdb.sqlparser.ast.SQLExpr;
import com.xdb.sqlparser.ast.SQLName;
import com.xdb.sqlparser.ast.SQLObject;
import com.xdb.sqlparser.ast.SQLPartitioningClause;
import com.xdb.sqlparser.ast.statement.SQLCreateTableStatement;
import com.xdb.sqlparser.ast.statement.SQLExprTableSource;
import com.xdb.sqlparser.ast.statement.SQLSelect;
import com.xdb.sqlparser.dialect.mysql.ast.MySqlObjectImpl;
import com.xdb.sqlparser.dialect.mysql.visitor.MySqlASTVisitor;
import com.xdb.sqlparser.visitor.SQLAstVisitor;
import com.xdb.util.JDBCConstants;

public class MySqlCreateTableStatement extends SQLCreateTableStatement implements MySqlStatement {

	private Map<String, SQLObject> tableOptions = new LinkedHashMap<String, SQLObject>();

	protected SQLSelect query;

	private SQLPartitioningClause partitioning;

	private List<SQLCommentHint> hints = new ArrayList<SQLCommentHint>();

	private List<SQLCommentHint> optionHints = new ArrayList<SQLCommentHint>();

	private SQLExprTableSource like;

	public MySqlCreateTableStatement() {
		super(JDBCConstants.MYSQL);
	}

	public SQLExprTableSource getLike() {
		return like;
	}

	public void setLike(SQLName like) {
		this.setLike(new SQLExprTableSource(like));
	}

	public void setLike(SQLExprTableSource like) {
		if (like != null) {
			like.setParent(this);
		}
		this.like = like;
	}

	public List<SQLCommentHint> getHints() {
		return hints;
	}

	public void setHints(List<SQLCommentHint> hints) {
		this.hints = hints;
	}

	public void setTableOptions(Map<String, SQLObject> tableOptions) {
		this.tableOptions = tableOptions;
	}

	public SQLPartitioningClause getPartitioning() {
		return partitioning;
	}

	public void setPartitioning(SQLPartitioningClause partitioning) {
		this.partitioning = partitioning;
	}

	public Map<String, SQLObject> getTableOptions() {
		return tableOptions;
	}

	public SQLSelect getQuery() {
		return query;
	}

	public void setQuery(SQLSelect query) {
		this.query = query;
	}

	@Override
	protected void accept0(SQLAstVisitor visitor) {
		if (visitor instanceof MySqlASTVisitor) {
			accept0((MySqlASTVisitor) visitor);
		} else {
			throw new IllegalArgumentException("not support visitor type : " + visitor.getClass().getName());
		}
	}

	public void accept0(MySqlASTVisitor visitor) {
		if (visitor.visit(this)) {
			this.acceptChild(visitor, getHints());
			this.acceptChild(visitor, getTableSource());
			this.acceptChild(visitor, getTableElementList());
			this.acceptChild(visitor, getLike());
			this.acceptChild(visitor, getQuery());
		}
		visitor.endVisit(this);
	}

	public static class TableSpaceOption extends MySqlObjectImpl {

		private SQLName name;
		private SQLExpr storage;

		public SQLName getName() {
			return name;
		}

		public void setName(SQLName name) {
			this.name = name;
		}

		public SQLExpr getStorage() {
			return storage;
		}

		public void setStorage(SQLExpr storage) {
			this.storage = storage;
		}

		@Override
		public void accept0(MySqlASTVisitor visitor) {
			if (visitor.visit(this)) {
				acceptChild(visitor, getName());
				acceptChild(visitor, getStorage());
			}
			visitor.endVisit(this);
		}

	}

	public List<SQLCommentHint> getOptionHints() {
		return optionHints;
	}

	public void setOptionHints(List<SQLCommentHint> optionHints) {
		this.optionHints = optionHints;
	}
}
